package com.jwell.panprovider.model

import com.jwell.driverapp.tools.BaseSubscriber
import com.jwell.panprovider.application.App
import com.jwell.panprovider.contract.LoginContract
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * Created by 76577 on 2018/3/26.
 * 登录
 */
class LoginModel : LoginContract.Model, BaseModel() {
    override fun sendMsgCode(phone: String, userSessionId: String, msgCode: String, baseSubscriber: BaseSubscriber) {
        val map = RequestSendMsg(phone, userSessionId, msgCode, 4)
        getApiStore().sendMsgCode(map)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(baseSubscriber)
    }

    override fun loginByPhoneCode(usernameOrEmailAddress: String, phone: String, phoneCode: String, baseSubscriber: BaseSubscriber) {
        val map = RequestLoginByPhone(usernameOrEmailAddress, phone, phoneCode, 4)
        getApiStore().loginByPhoneCode(map).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(baseSubscriber)
    }

    override fun login(userName: String, passWorld: String, baseSubscriber: BaseSubscriber) {
        val map = RequestData(userName, passWorld, 4)
        if (App.DEBUG) println("登录" + "usernameOrEmailAddress" + userName + "password" + passWorld)
        getApiStore().login(map)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(baseSubscriber)
    }

    data class RequestData(var usernameOrEmailAddress: String? = null,
                           var password: String? = null,
                           var userType: Int)

    data class RequestSendMsg(var phone: String? = null,
                              var userSessionId: String? = null,
                              var msgCode: String,
                              var userType: Int)

    data class RequestLoginByPhone(var usernameOrEmailAddress: String? = null,
                                   var phone: String? = null,
                                   var phoneCode: String,
                                   var userType: Int)
}